package com.sso.client.config;

import com.sso.core.conf.SSOConf;
import com.sso.core.filter.SsoTokenFilter;
import com.sso.core.filter.SsoWebFilter;
import com.sso.core.util.JedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SysConfig {

    /**
     * @Configuration 配置项代码
     */
    @Value("${sso.server}")
    private String ssoServer;

    @Value("${sso.client}")
    private String ssoClient;

    @Value("${sso.logout.path}")
    private String ssoLogoutPath;

    @Value("${sso.redis.address}")
    private String ssoRedisAddress;

    @Value("${sso.excluded.paths}")
    private String ssoExcludedPaths;




    @Bean
    public FilterRegistrationBean ssoFilterRegistration() {

        JedisUtil.init(ssoRedisAddress);

        FilterRegistrationBean registration = new FilterRegistrationBean();

        registration.setName("SsoWebFilter");
        registration.setOrder(1);
        registration.addUrlPatterns("/*");
        // vue 项目或 app 调用请使用 new SsoTokenFilter(),jsp或其它web建议使用 new SsoWebFilter();
        // registration.setFilter(new SsoTokenFilter());

        registration.setFilter(new SsoWebFilter());
        registration.addInitParameter(SSOConf.SSO_SERVER, ssoServer);
        registration.addInitParameter(SSOConf.REDIRECT_URL,ssoClient);
        registration.addInitParameter(SSOConf.SSO_LOGOUT_PATH, ssoLogoutPath);
        registration.addInitParameter(SSOConf.SSO_EXCLUDED_PATHS, ssoExcludedPaths);

        return registration;
    }
}
